*****
*****
***     This SAS run uses the April 18, 2005
***     release of the 2004 ANES.
*****
*****
;
filename in "INSERT PATH TO DATASET\nes04dat.txt";
libname out "INSERT PATH TO SAS LIBRARY";
options linesize = 72;

*
   This run uses the 2004 ANES to create a dataset
   in which each observation is an individual respondents
   evaluation of a single political figures. There are
   13 political figures, so the total number of observations
   in the new dataset is 13 times the number of survey
   respondents in the ANES.

   These data are passed to R, and used to estimate the
   multilevel model of candidate evaluation summarized in
   Table 2, and discussed in greater detail in the supplemental
   report, "A Multilevel Model Showing the Effects of Uncertainty
   on Individual Candidate Evaluations."
;

data nes04;
infile in lrecl=3347 firstobs=2;

input caseid 34-37
   gwbush 812-814
   kerry 816-818
   nader 820-822
   cheney 824-826
   edwards 828-830
   lbush 832-834
   hclinton 836-838
   bclinton 840-842
   powell 844-846
   ashcroft 848-850
   mccain 852-854
   dempty 856-858
   reppty 860-862
   rlibcon 1034-1035
   rpid 1104
;

if rpid in (0 1 2 3 4 5 6) then partyid = rpid;

*
   The cdim series gives the coordinates of the political
   figures on the first dimension of the MDS solution
   (which appears to tap ideological differences)
;

cdim1 =  1.28273;  
cdim2 = -1.74139;
cdim3 = -0.52001;
cdim4 =  1.64979;
cdim5 = -1.64139;  
cdim6 =  0.71376;  
cdim7 = -1.22772;
cdim8 = -0.97580;
cdim9 =  0.36254;
cdim10 =  1.69637;
cdim11 =  0.39573;
cdim12 = -1.38392;
cdim13 =  1.38931;

*
   The width series gives the width of each figures
   confidence ellipse in the direction of the first
   dimension in the solution.
;

width1 = 0.3986882;
width2 = 0.3679156;
width3 = 0.5922966;
width4 = 0.4682341;
width5 = 0.3185515;
width6 = 0.4267753;
width7 = 0.4237553;
width8 = 0.4895168;
width9 = 0.6993957;
width10 = 0.5704703;
width11 = 0.9971918;
width12 = 0.4823753;
width13 = 0.3661809;

*
   The nm series gives the names of the 13 
   political figures.
;

nm1 = "gwbush";
nm2 = "kerry";
nm3 = "nader";
nm4 = "cheney";
nm5 = "edwards";
nm6 = "lbush"; 
nm7 = "hclinton"; 
nm8 = "bclinton"; 
nm9 = "powell"; 
nm10 = "ashcroft";
nm11 = "mccain"; 
nm12 = "dempty"; 
nm13 = "reppty"; 

*
   Remove missing value codes and rescale
   respondent ideology to range from zero to
   six
;

if rlibcon < 1 or rlibcon > 7 then rlibcon = .;

rlibcon = rlibcon - 1;

array therms [13] gwbush--reppty;
array clc [13] cdim1-cdim13;
array width [13] width1-width13;
array copar [13] copar1-copar13;

   do i = 1 to 13;
   copar [i] = 0;
   end;

*
   Create dummy variables to indicate if respondent is
   same party as a candidate or not. For this purpose,
   nonleaning independents are considered co-partisans
   with Nader.
;

   if partyid in (0 1 2) then do;
   copar2 = 1;
   copar5 = 1;
   copar7 = 1;
   copar8 = 1;
   copar12 = 1;
   end;

   if partyid in (4 5 6) then do;
   copar1 = 1;
   copar4 = 1;
   copar6 = 1;
   copar9 = 1;
   copar10 = 1;
   copar11 = 1;
   copar13 = 1;
   end;

   if partyid = 3 then copar3 = 1;

*
   Set missing values for thermometers, and
   rescale candidate dimension coordinates and
   width of confidence ellipses so they are on
   a zero to six scale, comparable to respondent
   ideology.
;

addit = -min(of cdim1-cdim13);
rng = range(of cdim1-cdim13);

   do i = 1 to 13;
   if therms [i] < 0 or therms [i] > 100 then 
      therms [i] = .;
   clc[i] = ((clc[i] + addit)/rng) * 6;
   width[i] = (width[i] / rng) * 6;
   end;
run;

data ceval;    
set nes04;

*
   This dataset writes out one new observation for each evaluation
   from each survey respondent. The variable eval is the feeling
   thermometer score (centered at the neutral point, originally 
   50), variable dist is the distance from the respondents ideology
   to the candidates position on the first MDS dimension, variable 
   width is the width of the candidates confidence ellipse along
   the first dimension, copar is the copartisanship dummy variable,
   and interac is the multiplicative term between dist and width.
;

array therms [13] gwbush--reppty;
array clc [13] cdim1-cdim13;
array wide [13] width1-width13;
array nm [13] nm1-nm13;
array cp [13] copar1-copar13;

   do i = 1 to 13;
   eval = therms[i] - 50;
   dist = abs(rlibcon - clc[i]);
   width = wide[i];
   copar = cp[i];
   cname = nm[i];
   interac = dist * width;
   output;
   end;

keep caseid eval dist width 
   interac cname copar;
run;

*
   Estimate summary statistics for variables
   in ceval dataset,
;

proc means data = ceval;
var eval dist width;
run;

data out.ceval;
set ceval;
run;
